package fm.admin.controller;

import fm.cache.AreaCache;
import fm.controller.BaseController;
import fm.entity.Section;
import fm.mongoService.UserService;
import fm.yichenet.service.SectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 社区管理 控制器
 * Created by CM on 17/5/30.
 */
@Controller
@RequestMapping("/forum")
public class FourmMgrController extends BaseController {

    @Autowired
    SectionService sectionService;

    @Autowired
    UserService userService;

    @RequestMapping("/carousel")
    public String index(ModelMap modelMap) {
        modelMap.put("citys", AreaCache.getAllProvince());

        return "/admin/forum/carousel";
    }

    @RequestMapping("/article")
    public String article(ModelMap modelMap) {
        List merchantList = null;
        try {
            List<Section> sectionList = sectionService.getAllSection(1);
            modelMap.put("sectionList", sectionList);


            merchantList = userService.getAllMerchantUser();

            modelMap.put("merchantList", merchantList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/admin/forum/article";
    }


    @RequestMapping("/picArticle")
    public String picArticle(ModelMap modelMap) {
        List<Section> sectionList = sectionService.getAllSection(1);
        modelMap.put("sectionList", sectionList);
        return "/admin/forum/picArticle";
    }

    @RequestMapping("/section")
    public String section(ModelMap modelMap) {

        return "/admin/forum/section";
    }

    @ResponseBody
    @RequestMapping("/list")
    public Map getSectionList(Integer status) {
        Map res = new HashMap();
        try {
            List<Section> sectionList = sectionService.getAllSection(status);
            res.put("data",sectionList);
            this.success(res);
        } catch (Exception e) {
            LOGGER.error("occur un-expected error", e.getMessage());
            this.failed(res, "服务器发生未知错误，请稍后再试或者联系管理员解决！");
        }
        return res;
    }
}